package com.sm.healthy.ui.view.ecg;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;

import static android.graphics.Paint.Style.STROKE;

public class EndEcgPicture extends SurfaceView
        implements SurfaceHolder.Callback {


    public int pointIntArr[] = new int[500];

    private SurfaceHolder mHolder;
    private Path mPath;
    //折线画笔
    private Paint mPaint;
    //背景画笔
    private Paint mPaint2;

    Timer timer;
    public Canvas mCanvas;
    //初始化x轴起始位置
    private float x = 0;
    //矩形锁定区域x轴初始化
    private int xx = -10;
    //    private int xx = 0;
    //y轴初始化
    private float y = 100;
    //初始化标志
    private int tag = 0;

    public volatile boolean mTag = false;

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("finish".equals(intent.getAction())) {
                timer.cancel();
            }

        }
    };


    public EndEcgPicture(Context context) {
        super(context);
        initView();
    }

    public EndEcgPicture(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public EndEcgPicture(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {

        Arrays.fill(pointIntArr, 128);

        mHolder = getHolder();
        mHolder.addCallback(this);
        setFocusable(true);
        setFocusableInTouchMode(true);

        //设置surfaceView透明
        setZOrderOnTop(true);
        getHolder().setFormat(PixelFormat.TRANSLUCENT);

        this.setKeepScreenOn(true);
        mPath = new Path();

        //进行折线画笔的属性设置
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(STROKE);
        mPaint.setStrokeWidth(2.5f);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        //进行背景画笔的属性设置
        mPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint2.setColor(Color.RED);
        mPaint2.setStyle(STROKE);
        mPaint2.setStrokeWidth(1.0f);
        mPaint2.setAntiAlias(true);
        mPaint2.setStrokeCap(Paint.Cap.ROUND);
        mPaint2.setStrokeJoin(Paint.Join.ROUND);

        //接收广播
//        IntentFilter filter = new IntentFilter();
//        filter.addAction("finish");
//        filter.addAction("picData");
//        getContext().registerReceiver(mBroadcastReceiver, filter);
//

    }

    int widthData;
    float heightData;

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        widthData = getWidth() + 20;
        heightData = (float) (getHeight() / 3500.0);
        Log.i("nagesha", "执行初始化创建方法");
        timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                if (mTag) {
                    draw();
                    mTag = false;
                }
            }
        };
        timer.schedule(task, 1000, 30);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder,
                               int format, int width, int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.i("nagesha", "执行销毁方法");

    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        pointIntArr = new int[500];
        if (timer != null) {
            timer.cancel();
            timer.purge();
        }

    }

    int m = 0;
    int n = 0;
    Rect canvasRect = new Rect(xx, 0, xx, getBottom());

    //绘制方法
    public void draw() {
        if (mTag) {
            if (pointIntArr != null) {
                y = (pointIntArr[n] * getHeight()) / 255;
                n++;
                if (n == 500) {
                    n = 0;
                }

            } else {
                y = 0;
            }
            x += 2;
            xx += 2;
            mPath.lineTo(x, -y + (getHeight() * 5 / 4));
            tag += 2;
            //当画够500条数据之后发送消息给handler重新初始化
            if (tag >= widthData) {
                tag = 0;
                mPath = new Path();
                if (pointIntArr[0] != 0) {
                    mPath.moveTo(0, -y + (getHeight() * 5 / 4));
                } else {
                    mPath.moveTo(0, getHeight() * 5 / 4);
                }
                x = 0;
                xx = -10;
            }
            canvasRect.set(xx, 0, xx + 30, getBottom());
        } else canvasRect.set(-10, 0, widthData, getBottom());
        mCanvas = mHolder.lockCanvas(canvasRect);
        if (mCanvas != null) {
            mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

            //画心电图
            mCanvas.drawPath(mPath, mPaint);

            mHolder.unlockCanvasAndPost(mCanvas);

        }

    }

    public void reset() {
        m = 0;
        n = 0;
        pointIntArr = new int[500];
        Arrays.fill(pointIntArr, 128);
        x = 0;
        xx = -10;
        y = 100;
        tag = 0;
        mPath = new Path();
        mTag = false;
        draw();
    }
}
